/*
 * Created by Jaren at 2022/2/21 11:35
 */
import 'dart:core';

class BrowseFileMethod {

    static const int none = 0xFFFF;

    static const int methodFileAndDir = 0;
    static const int methodFileOnly = 1;
    static const int methodDirOnly = 2;

    static const int depthAll = 0;
    static const int pathCurrentDir = 1;

    static const int depthDefault = depthAll;

    static const pathDefault = "";
    static const nameDefault = "";

    static const int methodDefault = methodFileAndDir;

    int method;
    int depth;
    String path = pathDefault;
    String name = nameDefault;

    BrowseFileMethod(this.method, this.depth, this.path, this.name);

    String generate({bool brief = true}) {
        if (brief) {
            return method == none ? "" : method.toString() + (depth == none ? "" : "|" + depth.toString()) + "|" + path + "|" + name;
        } else {
            return method == none ? methodDefault.toString() : method.toString() + "|" + (depth == none ? depthDefault.toString() : depth.toString()) + "|" + path + "|" + name;
        }
    }
}

class BrowseFileMethodBuilder {
    int method = BrowseFileMethod.none;

    int depth = BrowseFileMethod.none;

    String path = BrowseFileMethod.pathDefault;

    String name = BrowseFileMethod.nameDefault;


    BrowseFileMethodBuilder fileAndDir() {
        method = BrowseFileMethod.methodFileAndDir;
        return this;
    }

    BrowseFileMethodBuilder fileOnly() {
        method = BrowseFileMethod.methodFileOnly;
        return this;
    }

    BrowseFileMethodBuilder directoryOnly() {
        method = BrowseFileMethod.methodDirOnly;
        return this;
    }

    BrowseFileMethodBuilder depthAll() {
        depth = BrowseFileMethod.depthAll;
        return this;
    }

    BrowseFileMethodBuilder depthCurrentDir() {
        depth = BrowseFileMethod.pathCurrentDir;
        return this;
    }

    BrowseFileMethodBuilder withPath(String path) {
        this.path = path;
        return this;
    }

    BrowseFileMethodBuilder fileType(String fileType) {
        name = formatFileType(fileType);
        return this;
    }

    BrowseFileMethodBuilder filenameContains(String name) {
        this.name = formatFilenameContaining(name);
        return this;
    }

    BrowseFileMethodBuilder filenameStartWith(String prefix) {
        name = formatFilenameStartWith(prefix);
        return this;
    }

    BrowseFileMethodBuilder filenameEndWith(String suffix) {
        name = formatFilenameEndWith(suffix);
        return this;
    }

    BrowseFileMethodBuilder filename(String name) {
        this.name = name;
        return this;
    }

    BrowseFileMethodBuilder filenameContainingAndFileType(String containing, String fileType) {
        name = formatFilenameContainingAndFileType(containing, fileType);
        return this;
    }

    BrowseFileMethod build() {
        return BrowseFileMethod(method, depth, path, name);
    }

    String formatFileType(String fileType) {
        return fileType == '' || fileType.startsWith(".") ? fileType : "." + fileType;
    }

    String formatFilenameContaining(String name) {
        if (name == '') {
            return name;
        }
        if (name.startsWith("*")) {
            if (name.endsWith("*")) {
                return name;
            }
            return name + "*";
        }

        if (name.endsWith("*")) {
            return "*" + name;
        }
        return "*" + name + "*";
    }

    String formatFilenameStartWith(String prefix) {
        if (prefix == '') {
            return prefix;
        }
        if (prefix.endsWith("*")) {
            return prefix;
        }
        return prefix + "*";
    }

    String formatFilenameEndWith(String suffix) {
        if (suffix == '') {
            return suffix;
        }
        if (suffix.startsWith("*")) {
            return suffix;
        }
        return "*" + suffix;
    }

    String formatFilenameContainingAndFileType(String containing, String fileType) {
        return formatFilenameContaining(containing) + formatFileType(fileType);
    }

}